


################
#Figure B-1
################

all_together5$republican <- (all_together5$party_code==200)*1

me_coefmaker <- function(r,timeper){
  m <- lm(loyalty ~ ideological_extremism_new + majority +
            baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
            freshman  + best_committee + power_committee + chair, 
          data=subset(all_together5, republican==r & congress==timeper & leader==0))
  return(c(coef(m)[2],sqrt(diag(vcov(m)))[2]))
}

vm_coefmaker <- function(r,timeper){
  m <- lm(responsiveness_to_party_calls ~ ideological_extremism_new + majority +
            baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
            freshman  + best_committee + power_committee + chair, 
          data=subset(all_together5, republican==r & congress==timeper & leader==0))
  return(c(coef(m)[2],sqrt(diag(vcov(m)))[2]))
}

me_reps <- t(sapply(101:112, me_coefmaker, r=1))
vm_reps <- t(sapply(101:112, vm_coefmaker, r=1))
me_dems <- t(sapply(101:112, me_coefmaker, r=0))
vm_dems <- t(sapply(101:112, vm_coefmaker, r=0))

plot_data <- data.frame(congress=rep(101:112,4),slopes=c(me_reps[,1],me_dems[,1],vm_reps[,1],vm_dems[,1]),
                        se=c(me_reps[,2],me_dems[,2],vm_reps[,2],vm_dems[,2]),
                        party=c(rep("Republicans",12),rep("Democrats",12),rep("Republicans",12),rep("Democrats",12)),
                        model=c(rep("IRT Loyalty",24),rep("Minozzi-Volden",24)))

vm_comparison <-
  ggplot(plot_data, aes(factor(congress),slopes,colour=factor(party))) + 
  geom_point(position=position_dodge(width=0.5)) +
  geom_linerange(position=position_dodge(width=0.5),aes(ymin=slopes-1.96*se,ymax=slopes+1.96*se)) +
  facet_wrap(~model, scales = "free", nrow = 2) +
  geom_hline(yintercept = 0, linetype="dashed") +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  ylab("Slope on Ideological Extremism") +
  xlab("Congress") +
  theme_bw()

##present the plot

ggsave(vm_comparison, file = paste0(getwd(), "/FigureB1.pdf"), height = 6, width=9)


################
#Figure C-1
################


x.test.new <- data.frame("loyalty"=rep(seq(0,100,length=101), each=3), "cdist"=rep(c(0,1,2)),
                         "qual_chal"=0, "presparty"=1, "midterm"=1, "presvote"=55,
                         "freshman"=0, "income_change"=1.6, "inc_spending_adv"=1,
                         "inc_vote_lag"=55, "pres_pty_approval"=0, "elyr"=1994)

x.test.pu <- data.frame("party_unity"=rep(seq(0,100,length=101), each=3), "cdist"=rep(c(0,1,2)),
                        "qual_chal"=0, "presparty"=1, "midterm"=1, "presvote"=55,
                        "freshman"=0, "income_change"=1.6, "inc_spending_adv"=1,
                        "inc_vote_lag"=55, "pres_pty_approval"=0, "elyr"=1994)

res.new <- data.frame("loyalty"=rep(seq(0,100,length=101), each=3),
                      "Ideology"=rep(c("Moderate","Mainstream","Extreme")),
                      "preds"=predict(mod_new, newdata=x.test.new),
                      "se"=predict(mod_new, newdata=x.test.new, se.fit=TRUE)$se)

res.pu <- data.frame("party_unity"=rep(seq(0,100,length=101), each=3),
                     "Ideology"=rep(c("Moderate","Mainstream","Extreme")),
                     "preds"=predict(mod_pu, newdata=x.test.pu),
                     "se"=predict(mod_pu, newdata=x.test.pu, se.fit=TRUE)$se)

res.new$mod <- "IRT Loyalty"
res.pu$mod <- "Party Unity"
colnames(res.pu)[1] <- "loyalty"

res.tog <- rbind(res.new,res.pu)

pu_vs_irt <-
  ggplot(na.omit(res.tog), aes(loyalty,preds,fill=Ideology,colour=Ideology,linetype=Ideology)) +
  geom_line() +
  geom_ribbon(aes(ymin=preds-1.96*se,ymax=preds+1.96*se), alpha=0.3) +
  xlab("Loyalty Measure") +
  ylab("Predicted Voteshare") +
  facet_wrap(~mod,scales = "free") +
  theme_bw()

##present the plot

ggsave(pu_vs_irt, file = paste0(getwd(), "/FigureC1.pdf"), height = 8, width=8)


################
#Figure E-1
################


ideals_over_time <-
  ggplot(all_together, aes(ideals_base,ideals,colour=as.factor(party_code),fill=as.factor(party_code))) +
  geom_point() +
  stat_smooth(method="lm", colour="grey") +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_fill_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_shape_discrete(name="Party", labels=c("Democrats","Republicans")) +
  xlab("Ideal Point (Canonical IRT)") +
  ylab("Ideal Point (IRT with Loyalty)") +
  facet_wrap(~congress,scales="free") +
  theme_bw()

##present the plot

ggsave(ideals_over_time, file = paste0(getwd(), "/FigureE1.pdf"),  width = 12, height = 10)




################
#Figure E-2
################

thiscong <- thiscong[order(thiscong$loyalty,decreasing=TRUE),]
thiscong$rsplit <- 2
thiscong$rsplit[round(nrow(thiscong)/2):nrow(thiscong)] <- 1

unity_scores_boehner <- 
  ggplot(thiscong, 
         aes(party_unity,reorder(factor(bioname),-party_unity),colour=as.factor(vote_against_boehner),fill=as.factor(vote_against_boehner))) +
  geom_point(size=3) +
  scale_color_manual(name="", values=c("purple","red"), labels=c("For Boehner","Against Boehner")) +
  scale_fill_manual(name="", values=c("purple","red"), labels=c("For Boehner","Against Boehner")) +
  xlab("Traditional Party Unity") +
  #geom_vline(aes(xintercept=0), colour="black", linetype="dashed") +
  ylab("") +
  facet_wrap(~rsplit, scales = "free") + 
  theme_bw() +
  theme(strip.background = element_blank(), strip.text = element_blank())

##present the plot


ggsave(unity_scores_boehner, file = paste0(getwd(), "/FigureE2.pdf"), width = 12, height = 10)


################
#Figure E-3
################

pu_over_time <- 
  ggplot(all_together, aes(ideals,party_unity,colour=as.factor(party_code),fill=as.factor(party_code))) +
  geom_point() +
  #stat_smooth(method="lm") +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_fill_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_shape_discrete(name="Party", labels=c("Democrats","Republicans")) +
  xlab("Ideal Point") +
  ylab("Party Unity Score") +
  facet_wrap(~congress,scales="free") +
  theme_bw()

##present the plot

ggsave(pu_over_time, file = paste0(getwd(), "/FigureE3.pdf"), width = 12, height = 10)



################
#Figure E-4
################

loyalty_over_time <-
  ggplot(all_together, aes(ideals,loyalty,colour=as.factor(party_code),fill=as.factor(party_code))) +
  geom_point() +
  #stat_smooth(method="lm") +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_fill_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_shape_discrete(name="Party", labels=c("Democrats","Republicans")) +
  xlab("Ideal Point") +
  ylab("Party Loyalty") +
  facet_wrap(~congress,scales="free") +
  theme_bw()

##present the plot

ggsave(loyalty_over_time, file = paste0(getwd(), "/FigureE4.pdf"), width = 12, height = 10)



################
#Figure E-5
################

loyalty_scores_boehner <- 
  ggplot(thiscong, 
         aes(loyalty,reorder(factor(bioname),-loyalty),colour=as.factor(vote_against_boehner),fill=as.factor(vote_against_boehner))) +
  geom_point(size=3) +
  geom_segment(aes(y=reorder(factor(bioname),-loyalty),yend=reorder(factor(bioname),-loyalty),x=loyalty_low,xend=loyalty_high)) +
  scale_color_manual(name="", values=c("purple","red"), labels=c("For Boehner","Against Boehner")) +
  scale_fill_manual(name="", values=c("purple","red"), labels=c("For Boehner","Against Boehner")) +
  scale_shape_discrete(name="", labels=c("Rest of Republicans","Freedom Caucus")) +
  xlab("Party Effect") +
  geom_vline(aes(xintercept=50), colour="black", linetype="dashed") +
  ylab("") +
  facet_wrap(~rsplit, scales = "free") + 
  theme_bw() +
  theme(strip.background = element_blank(), strip.text = element_blank())

##present the plot

ggsave(loyalty_scores_boehner, file = paste0(getwd(), "/FigureE5.pdf"), width = 12, height = 10)



################
#Figure E-6
################

loyalty_scores <- 
  ggplot(thiscong, 
         aes(loyalty,reorder(factor(bioname),-loyalty),colour=as.factor(freedom_caucus),fill=as.factor(freedom_caucus))) +
  geom_point(size=3) +
  geom_segment(aes(y=reorder(factor(bioname),-loyalty),yend=reorder(factor(bioname),-loyalty),x=loyalty_low,xend=loyalty_high)) +
  scale_color_manual(name="", values=c("purple","red"), labels=c("Rest of Republicans","Freedom Caucus")) +
  scale_fill_manual(name="", values=c("purple","red"), labels=c("Rest of Republicans","Freedom Caucus")) +
  scale_shape_discrete(name="", labels=c("Rest of Republicans","Freedom Caucus")) +
  xlab("Party Effect") +
  geom_vline(aes(xintercept=50), colour="black", linetype="dashed") +
  ylab("") +
  facet_wrap(~rsplit, scales = "free") + 
  theme_bw() +
  theme(strip.background = element_blank(), strip.text = element_blank())

##present the plot

ggsave(loyalty_scores, file = paste0(getwd(), "/FigureE6.pdf"), width = 12, height = 10)

cat("Replication file 8 done.","\n")
